{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('/home/tra/Downloads/spark-2.3.2-bin-hadoop2.7/python/') # Not conda\n",
    "#Careful with Java version\n",
    "#conda install py4j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyspark as spark\n",
    "from pyspark.sql.functions import col,round as round_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = spark.SparkContext('spark://127.0.1.1:7077')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "sqlc = spark.SQLContext(sc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_df = sqlc.read.parquet('geno.parquet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pyspark.sql.dataframe.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "print(type(all_df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+------+-----+\n",
      "|window|count|\n",
      "+------+-----+\n",
      "|   0.0|   43|\n",
      "|   1.0|  475|\n",
      "|   2.0|  858|\n",
      "|   3.0| 1146|\n",
      "|   4.0|  915|\n",
      "|   6.0|  746|\n",
      "|   7.0| 1420|\n",
      "|   8.0| 1167|\n",
      "|   9.0|   32|\n",
      "|  10.0|  255|\n",
      "|  11.0|  471|\n",
      "|  12.0|  110|\n",
      "|  13.0| 1336|\n",
      "|  14.0|   75|\n",
      "|  15.0| 1770|\n",
      "|  16.0| 1693|\n",
      "|  17.0| 2251|\n",
      "|  18.0| 1157|\n",
      "|  19.0|  668|\n",
      "|  20.0|  898|\n",
      "+------+-----+\n",
      "only showing top 20 rows\n",
      "\n"
     ]
    }
   ],
   "source": [
    "all_df.select(col(\"*\"), round_(col(\"POS\") / 50000).alias('window')).groupBy('window').count().orderBy('window').show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
